package validates

import (
	"myapp/dal/model"
	"myapp/dal/query"
	"strconv"
	"strings"
)

type Member struct {
	ID                int                    `json:"id"`
	Name              string                 `json:"name" binding:"required" label:"名称"`    // 必填字段
	Content           string                 `json:"content" binding:"required" label:"内容"` // 必填字段
	MemberCategoryIds int                    `json:"member_category_ids" label:"合作类型"`      // 合作类型
	Categories        []model.MemberCategory `json:"categories" label:"分类"`
	Services          string                 `json:"services" binding:"required" label:"主营业务"`

	NameEn     string `json:"name_en" label:"name"`       // 必填字段
	ContentEn  string `json:"content_en" label:"content"` // 必填字段
	ServicesEn string `json:"services_en" label:"services_en"`
}

// 处理函数：将 `Need` 转换为 `CustomNeed`，并添加额外字段
func ProcessMember(nd *model.Member) Member {
	// 假设查询分类信息的函数是这样
	var CategoryIds []int
	if len(nd.MemberCategoryIds) > 0 {
		categoryIds := strings.Split(nd.MemberCategoryIds, ",")
		for _, v := range categoryIds {
			vid, _ := strconv.Atoi(v)
			CategoryIds = append(CategoryIds, vid)
		}
	}
	var categories []model.MemberCategory
	if len(nd.MemberCategoryIds) > 0 {
		categories, _ = GetMemberCategoryByIDs(CategoryIds) // 简化的错误处理
	}

	return Member{
		ID:         int(nd.ID),
		Name:       nd.Name,
		Categories: categories,
		NameEn:     nd.NameEn,
		Content:    nd.Content,
		ContentEn:  nd.ContentEn,
		Services:   nd.Services,
		ServicesEn: nd.ServicesEn,
	}
}

// 获取分类信息的示例函数
func GetMemberCategoryByIDs(categoryIDs []int) ([]model.MemberCategory, error) {
	// 查询并返回分类数据
	// 假设返回一个简单的分类列表
	var categories []model.MemberCategory
	// 模拟返回
	for _, id := range categoryIDs {
		category, _ := query.MemberCategory.Where(query.MemberCategory.ID.Eq(int32(id))).Take()
		if category != nil {
			categoryp, _ := query.MemberCategory.Where(query.MemberCategory.ID.Eq(category.Pid)).Take()
			if categoryp != nil {
				category.Name = categoryp.Name + "：" + category.Name
				category.NameEn = categoryp.NameEn + "：" + category.NameEn
			}

			categories = append(categories, *category)
		}
	}
	return categories, nil
}
